﻿package morn.core.components
{
    import com.core.*;
    import flash.display.*;
    import flash.events.*;
    import morn.core.events.*;
    import morn.core.utils.*;

    public class Component extends Sprite implements IComponent
    {
        protected var _width:Number;
        protected var _height:Number;
        protected var _contentWidth:Number = 0;
        protected var _contentHeight:Number = 0;
        protected var _disabled:Boolean;
        protected var _tag:Object;
        protected var _comXml:Object;
        protected var _dataSource:Object;
        protected var _toolTip:Object;
        protected var _mouseChildren:Boolean;
        public var runtime:String;
        public var gname:String;
        public var cached:Boolean = false;

        public function Component()
        {
            var _loc_1:Boolean = false;
            tabChildren = false;
            var _loc_1:* = _loc_1;
            tabEnabled = _loc_1;
            this.mouseChildren = _loc_1;
            this.preinitialize();
            this.createChildren();
            this.initialize();
            return;
        }// end function

        protected function preinitialize() : void
        {
            return;
        }// end function

        protected function createChildren() : void
        {
            return;
        }// end function

        protected function initialize() : void
        {
            return;
        }// end function

        public function callLater(param1:Function, param2:Array = null) : void
        {
            App.render.callLater(param1, param2);
            return;
        }// end function

        public function exeCallLater(param1:Function) : void
        {
            App.render.exeCallLater(param1);
            return;
        }// end function

        public function sendEvent(param1:String, param2 = null) : void
        {
            if (hasEventListener(param1))
            {
                dispatchEvent(new UIEvent(param1, param2));
            }
            return;
        }// end function

        public function remove() : void
        {
            if (parent)
            {
                parent.removeChild(this);
            }
            return;
        }// end function

        public function removeChildByName(param1:String) : void
        {
            var _loc_2:* = getChildByName(param1);
            if (_loc_2)
            {
                removeChild(_loc_2);
            }
            return;
        }// end function

        public function setPosition(param1:Number, param2:Number) : void
        {
            this.x = param1;
            this.y = param2;
            return;
        }// end function

        override public function set x(param1:Number) : void
        {
            super.x = int(param1);
            return;
        }// end function

        override public function set y(param1:Number) : void
        {
            super.y = int(param1);
            return;
        }// end function

        override public function get width() : Number
        {
            if (!isNaN(this._width))
            {
                return this._width;
            }
            if (this._contentWidth != 0)
            {
                return this._contentWidth;
            }
            return this.measureWidth;
        }// end function

        public function get displayWidth() : Number
        {
            return this.width * scaleX;
        }// end function

        protected function get measureWidth() : Number
        {
            var _loc_3:DisplayObject = null;
            this.commitMeasure();
            var _loc_1:Number = 0;
            var _loc_2:* = numChildren - 1;
            while (_loc_2 > -1)
            {
                
                _loc_3 = getChildAt(_loc_2);
                if (_loc_3.visible)
                {
                    _loc_1 = Math.max(_loc_3.x + _loc_3.width * _loc_3.scaleX, _loc_1);
                }
                _loc_2 = _loc_2 - 1;
            }
            return _loc_1;
        }// end function

        override public function set width(param1:Number) : void
        {
            if (this._width != param1)
            {
                this._width = param1;
                this.callLater(this.changeSize);
            }
            return;
        }// end function

        override public function get height() : Number
        {
            if (!isNaN(this._height))
            {
                return this._height;
            }
            if (this._contentHeight != 0)
            {
                return this._contentHeight;
            }
            return this.measureHeight;
        }// end function

        public function get displayHeight() : Number
        {
            return this.height * scaleY;
        }// end function

        protected function get measureHeight() : Number
        {
            var _loc_3:DisplayObject = null;
            this.commitMeasure();
            var _loc_1:Number = 0;
            var _loc_2:* = numChildren - 1;
            while (_loc_2 > -1)
            {
                
                _loc_3 = getChildAt(_loc_2);
                if (_loc_3.visible)
                {
                    _loc_1 = Math.max(_loc_3.y + _loc_3.height * _loc_3.scaleY, _loc_1);
                }
                _loc_2 = _loc_2 - 1;
            }
            return _loc_1;
        }// end function

        override public function set height(param1:Number) : void
        {
            if (this._height != param1)
            {
                this._height = param1;
                this.callLater(this.changeSize);
            }
            return;
        }// end function

        override public function set scaleX(param1:Number) : void
        {
            super.scaleX = param1;
            this.callLater(this.changeSize);
            return;
        }// end function

        override public function set scaleY(param1:Number) : void
        {
            super.scaleY = param1;
            this.callLater(this.changeSize);
            return;
        }// end function

        public function commitMeasure() : void
        {
            return;
        }// end function

        protected function changeSize() : void
        {
            this.sendEvent(Event.RESIZE);
            return;
        }// end function

        public function setSize(param1:Number, param2:Number) : void
        {
            this.width = param1;
            this.height = param2;
            return;
        }// end function

        public function set scale(param1:Number) : void
        {
            var _loc_2:* = param1;
            this.scaleY = param1;
            this.scaleX = _loc_2;
            return;
        }// end function

        public function get scale() : Number
        {
            return scaleX;
        }// end function

        public function get disabled() : Boolean
        {
            return this._disabled;
        }// end function

        public function set disabled(param1:Boolean) : void
        {
            if (this._disabled != param1)
            {
                this._disabled = param1;
                mouseEnabled = !param1;
                super.mouseChildren = param1 ? (false) : (this._mouseChildren);
                ObjectUtils.gray(this, this._disabled);
            }
            return;
        }// end function

        override public function set mouseChildren(param1:Boolean) : void
        {
            var _loc_2:* = param1;
            super.mouseChildren = param1;
            this._mouseChildren = _loc_2;
            return;
        }// end function

        public function get tag() : Object
        {
            return this._tag;
        }// end function

        public function set tag(param1:Object) : void
        {
            this._tag = param1;
            return;
        }// end function

        public function showBorder(param1:uint = 16711680) : void
        {
            this.removeChildByName("border");
            var _loc_2:* = new Shape();
            _loc_2.name = "border";
            _loc_2.graphics.lineStyle(1, param1);
            _loc_2.graphics.drawRect(0, 0, this.width, this.height);
            addChild(_loc_2);
            return;
        }// end function

        public function get comXml() : Object
        {
            return this._comXml;
        }// end function

        public function set comXml(param1:Object) : void
        {
            this._comXml = param1;
            return;
        }// end function

        public function get dataSource() : Object
        {
            return this._dataSource;
        }// end function

        public function set dataSource(param1:Object) : void
        {
            var _loc_2:String = null;
            this._dataSource = param1;
            for (_loc_2 in this._dataSource)
            {
                
                if (hasOwnProperty(_loc_2))
                {
                    this[_loc_2] = this._dataSource[_loc_2];
                }
            }
            return;
        }// end function

        public function get toolTip() : Object
        {
            return this._toolTip;
        }// end function

        public function set toolTip(param1:Object) : void
        {
            if (this._toolTip != param1)
            {
                this._toolTip = param1;
                if (Boolean(param1))
                {
                    addEventListener(MouseEvent.ROLL_OVER, this.onRollMouse);
                    addEventListener(MouseEvent.ROLL_OUT, this.onRollMouse);
                }
                else
                {
                    removeEventListener(MouseEvent.ROLL_OVER, this.onRollMouse);
                    removeEventListener(MouseEvent.ROLL_OUT, this.onRollMouse);
                }
            }
            return;
        }// end function

        protected function onRollMouse(event:MouseEvent) : void
        {
            if (event.type == MouseEvent.ROLL_OVER)
            {
                ModelProxy.change("tips", "normal", {data:this._toolTip});
            }
            else
            {
                ModelProxy.change("tips", "state", "close");
            }
            return;
        }// end function

    }
}
